using ASP;
using HoWave.Alipay;
using HoWave.Common;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
public class payvip_return_url : Member, IRequiresSessionState
{
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		SortedDictionary<string, string> requestGet = this.GetRequestGet();
		if (requestGet.Count > 0)
		{
			Notify notify = new Notify();
			bool flag = notify.Verify(requestGet, base.Request.QueryString["notify_id"], base.Request.QueryString["sign"]);
			if (flag)
			{
				string text = base.Request.QueryString["trade_no"];
				string text2 = base.Request.QueryString["out_trade_no"];
				string value = base.Request.QueryString["total_fee"];
				string text3 = base.Request.QueryString["subject"];
				string text4 = base.Request.QueryString["body"];
				string text5 = base.Request.QueryString["buyer_email"];
				string text6 = base.Request.QueryString["trade_status"];
				string text7 = base.Request.QueryString["extra_common_param"];
				if (base.Request.QueryString["trade_status"] == "TRADE_FINISHED" || base.Request.QueryString["trade_status"] == "TRADE_SUCCESS")
				{
					PayOnline payOnline = new PayOnline();
					payOnline.SetPayOK(text2);
					string[] array = text7.Split(new char[]
					{
						'-'
					});
					string[] array2 = array[1].Split(new char[]
					{
						'_'
					});
					int num = Convert.ToInt32(array2[0]);
					decimal num2 = Convert.ToDecimal(array2[1]);
					decimal d = Convert.ToDecimal(value);
					if (d != num2 * num)
					{
						this.WriteErrMsg("", Language.GetMemberMsg("Voucher/DataIllegal", new string[0]), "");
					}
					int cardStoreID = Convert.ToInt32(array[0]);
					int num3 = (int)this.conn.GetScalar("Select UserID From HL_PayOnline WHERE PayNum= '" + text2 + "'", new IDbDataParameter[0]);
					if (!this.AddUserVipBuyedCard(num, cardStoreID, num2))
					{
						this.WriteErrMsg("", Language.GetMemberMsg("Voucher/AddVipError", new string[0]), "");
					}
					base.Response.Redirect("~/Member/PayOneline.aspx");
				}
				else
				{
					base.Response.Write("trade_status=" + base.Request.QueryString["trade_status"] + "支付操作未完成！");
				}
			}
			else
			{
				base.Response.Write("验证失败！请与网站管理员联系。");
			}
		}
		else
		{
			base.Response.Write("无返回参数");
		}
	}
	public SortedDictionary<string, string> GetRequestGet()
	{
		SortedDictionary<string, string> sortedDictionary = new SortedDictionary<string, string>();
		NameValueCollection queryString = base.Request.QueryString;
		string[] allKeys = queryString.AllKeys;
		for (int i = 0; i < allKeys.Length; i++)
		{
			sortedDictionary.Add(allKeys[i], base.Request.QueryString[allKeys[i]]);
		}
		return sortedDictionary;
	}
	private bool AddUserVipBuyedCard(int count, int CardStoreID, decimal FaceValue)
	{
		bool flag = false;
		bool result;
		if (count < 1 || CardStoreID < 1 || FaceValue < 0m)
		{
			result = false;
		}
		else
		{
			HoWave.Data.Model.UserCardStore model = Blls.bllUserCardStore.GetModel(CardStoreID);
			if (model == null)
			{
				flag = false;
			}
			else
			{
				HoWave.Data.Model.UserVipCard userVipCard = new HoWave.Data.Model.UserVipCard();
				UserCardRecordModel userCardRecordModel = new UserCardRecordModel();
				userCardRecordModel.Amount = model.FaceValue;
				userCardRecordModel.Remark = "购买VIP卡";
				userCardRecordModel.CardType = "1";
				userCardRecordModel.UserID = new int?(base.user.UserID);
				userVipCard.CardCategory = model.CardCategory;
				userVipCard.FaceValue = model.FaceValue;
				userVipCard.Balance = model.FaceValue;
				userVipCard.PurchaseDate = new DateTime?(DateTime.Now);
				userVipCard.PurchaseUserID = new int?(base.user.UserID);
				userVipCard.CouponAmount = model.FaceValue * 0.01m * Convert.ToDecimal(model.DiscountRate);
				for (int i = 0; i < count; i++)
				{
					long num = Blls.bllUserVipCard.Add(userVipCard);
					this.conn.Execute(string.Concat(new object[]
					{
						"UPDATE [HL_User] SET [Balance] = [Balance]+",
						userVipCard.CouponAmount,
						" WHERE [UserID] = ",
						base.user.UserID
					}));
					userCardRecordModel.CardNumber = Conn.GetSingle("select Top 1 CardNumber from MBF_UserVipCard where id=" + num, new IDbDataParameter[0]).IsNull(new object[0]);
					if (userCardRecordModel.CardNumber != "")
					{
						Blls.bllUserCardRecord.Add(userCardRecordModel);
					}
					flag = true;
				}
			}
			result = flag;
		}
		return result;
	}
}
